// shopPages/shoplist/shoplist.js
var sliderWidth = 70;
var app = getApp();
var selectedNews = false;
var newsList = [];
// var page = 1;
Page({
  /**
   * 页面的初始数据
   */
  data: {

    s_list: [],
    s_list1: [],
    s_list2: [],
    s_list3: [],

    // 选项卡
    tabs: ["综合", "价格", "抵扣", "销量"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    // goodsParam:[],
    page: 1,
    // top_show_price: true,
    // top_show_integral: true,
    // top_show_sale_num: true,
    // bottom_show_price:false,
    // bottom_show_integral: false,
    // bottom_show_sale_num: false,
    // hui_show:true,
    hui0_show: true,
    hui1_show: true,
    hui2_show: true,
    top0_show: false,
    top1_show: false,
    top2_show: false,
    currentSelectTripType: 'one',
    limited:0
  },
  selectedZH: function (e) {
    var that = this;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui0_show: true,
      hui1_show: true,
      hui2_show: true,
    })
    var goodsParam = that.data.goodsParam;
    goodsParam.sort = '';
    that.setData({
      goodsParam: goodsParam
    })
    that.getGoodsList(goodsParam,1);
  },
  selectedJG: function (e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui0_show: false,
      hui1_show: true,
      hui2_show: true,
    })
    if (that.data.top0_show) {
      goodsParam.sort = 'price desc';
      that.setData({
        top0_show: false,
        goodsParam: goodsParam
      })
    } else {

      goodsParam.sort = 'price asc';
      that.setData({
        top0_show: true,
        goodsParam: goodsParam
      })
    }
    that.getGoodsList(goodsParam, 1);

  },
  selectedDK: function (e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui1_show: false,
      hui0_show: true,
      hui2_show: true
    })
    if (that.data.top1_show) {
      goodsParam.sort = 'integral_price desc';
      that.setData({
        top1_show: false,
        goodsParam: goodsParam
      })
    } else {
      goodsParam.sort = 'integral_price asc';
      that.setData({
        top1_show: true,
        goodsParam: goodsParam
      })
    }
    that.getGoodsList(goodsParam, 1);
  },
  selectedXL: function (e) {
    var that = this
    var goodsParam = that.data.goodsParam;
    that.setData({
      currentSelectTripType: e.currentTarget.dataset.id,
      hui2_show: false,
      hui1_show: true,
      hui0_show: true
    })
    if (that.data.top2_show) {
      goodsParam.sort = 'mount desc';
      that.setData({
        top2_show: false,
        goodsParam: goodsParam
      })
    } else {
      goodsParam.sort = 'mount asc';
      that.setData({
        top2_show: true,
        goodsParam: goodsParam
      })
    }
    that.getGoodsList(goodsParam, 1);
  },

  bindshop: function (event) {
    var classify = event.currentTarget.dataset.classify;
    var index = event.currentTarget.dataset.id;
    var that = this;

    var hui_show = that.data.hui_show;

    // console.log(top_show);
    // console.log(hui_show)
    console.log(that.data.top_show_price)
    if (index == 1) {
      var top_show_price = that.data.top_show_price;
      if (top_show_price) {
        that.setData({
          top_show_price: false,
          bottom_show_price: true,
        });
      } else {
        that.setData({
          top_show_price: true,
          bottom_show_price: false,
        });
      }
    } else if (index == 2) {
      var top_show_integral = that.data.top_show_integral;
      if (top_show_integral) {
        that.setData({
          top_show_integral: false,
          bottom_show_integral: false,
        });
      } else {
        that.setData({
          top_show_integral: false,
          bottom_show_integral: true,
        });
      }
    } else if (index == 3) {
      var bottom_show_sale_num = that.data.bottom_show_sale_num;
      if (bottom_show_sale_num) {
        that.setData({
          bottom_show_sale_num: false,
          bottom_show_sale_num: false,
        });
      } else {
        that.setData({
          bottom_show_sale_num: false,
          bottom_show_sale_num: true,
        });
      }
    }
    console.log(classify)  //输出的结果就是你点击的
    this.setData({
      shopitem: classify,  //更新

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var title='';
    if (options.limited!=undefined){
      that.setData({
        limited: options.limited
      });
    }
    var act_type = 0;
    switch (options.state){
      case "1":
        title='大促销';break;
      case "2":
        title = '限时抢购'; break;
      case "3":
        title = '满减活动'; break;
      case "4":
        title = '优惠专区'; break;
      case "5":
        if (options.act_type == 1){
          title = '新品首发';
        } else if (options.act_type == 2){
          title = '清仓特卖';
        } else if (options.act_type == 3) {
          title = '私人定制';
        } else if (options.act_type == 4) {
          title = '人气推荐';
        } else if (options.act_type == 5) {
          title = '越买越开心';
        } else if (options.act_type == 6) {
          title = '主题精选';
        } else if (options.act_type == 7) {
          title = '必买清单';
        } else if (options.act_type == 8) {
          title = '发现好货';
        }
        act_type = options.act_type;
     
    }
    console.log(title);
    wx.setNavigationBarTitle({
      title: title,
    })
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    var goodsParam = { state: options.state, sort: '', act_type: act_type};
    that.setData({
      goodsParam: goodsParam
    });
    selectedNews = false;
    newsList = [];

    that.getGoodsList(goodsParam, 0);
  },
  //获取商品列表
  getGoodsList: function (param,typep) {
    var that = this;
    
    var page = that.data.page;
    if (typep > 0){
      newsList = [];
      page = 1;
      that.setData({
        page: 1
      })
    }
    // 
    var url2 = '/mall/activity_goods';
  
    param.pageno = page;
    param.pagesize = 20;
    wx.showLoading({
      title: '玩命加载中',
    })
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function (res) {
      console.log(res)
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          goodsList: []
        })
      } else {
        var goodsList = res.data;
        for (let i = 0; i < goodsList.length; i++) {
          newsList.push(goodsList[i]);
        }
        if (goodsList.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
          that.setData({
            page: page
          });
        }
        that.setData({
          goodsList: newsList
        })

      }
      wx.hideLoading();
    });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  onReachBottom: function () {

    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      // wx.showLoading({
      //   title: '玩命加载中',
      // })
      // 页数+1  
      that.getGoodsList(that.data.goodsParam,0);

    } else {
      wx.hideLoading();
    }
  },
  toGoodsDetail_: function (e) {
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
    })
  }
})